var vleState = null;
function VLE_STATE() {
	this.visitedNodes = [];  // array of NODE_VISIT objects
}

VLE_STATE.prototype.getCurrentNodeVisit = function() {
	if (this.visitedNodes.length == 0) {
		return null;
	} else {
		return this.visitedNodes[this.visitedNodes.length - 1];
	}
}

function NODE_VISIT(node) {
	this.node = node;
	this.visitStartTime = new Date();
	this.visitEndTime = null;
}

/**
 * Sets a new NODE_VISIT, containing info on where the student 
 * is current on.
 */
VLE_STATE.prototype.setCurrentNodeVisit = function(node) {
	// set endtime on previous 
	var currentNode = this.getCurrentNodeVisit();   // currentNode becomes lastnode
	if (currentNode != null) {
		currentNode.visitEndTime = new Date();
	}
	var newNodeVisit = new NODE_VISIT(node);
	this.visitedNodes.push(newNodeVisit);
}

function VLE() {
	this.project = null;
	this.navigationLogic = null;
	this.navigationPanel = null;
	this.contentPanel = null;
	
	vleState = new VLE_STATE();
}

/**
 * Sets the Project to render
 */
VLE.prototype.setProject = function(project) {
	this.project = project;
	this.navigationPanel = new NavigationPanel(project.rootNode);
	this.contentPanel = new ContentPanel(project, project.rootNode);
}

VLE.prototype.renderNode = function(nodeId) {
	var nodeToVisit = this.project.rootNode.getNodeById(nodeId);
	if (this.navigationLogic == null || this.navigationLogic.canVisitNode(nodeToVisit)) {
		var currentNode = nodeToVisit;
		vleState.setCurrentNodeVisit(currentNode);
		this.navigationPanel.render();
		this.contentPanel.render(nodeId);
	}
}

VLE.prototype.getNodeVisitedInfo = function() {
	var infoInHtml = "";
	for (var i=0; i < vleState.visitedNodes.length; i++) {
		var currNode = vleState.visitedNodes[i];
		infoInHtml += "nodeId: " + currNode.node.id + "<br/>startTime: " + currNode.visitStartTime + "<br/>endTime: " + currNode.visitEndTime + "<br/><br/>";
	}
	document.getElementById("experimentaloutput").innerHTML = infoInHtml;
}

function NavigationPanel(rootNode) {
	this.rootNode = rootNode;
}

NavigationPanel.prototype.render = function() {
	var navHtml = this.rootNode.getNavigationHtml();
	document.getElementById("navigationDiv").innerHTML = navHtml;
}

function ContentPanel(project, rootNode) {
	this.project = project;
	this.rootNode = rootNode;
}

ContentPanel.prototype.render = function(nodeId) {
	var node = this.rootNode.getNodeById(nodeId);
	document.getElementById("contentDiv").innerHTML = node.getContentHtml();	
}

function NAVIGATION_LOGIC() {
}

NAVIGATION_LOGIC.prototype.canVisitNode = function(nodeToVisit) {
	return true;
}